import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_admin/api/odoo/dynamic_data_api.dart';
import 'package:flutter_admin/api/odoo/product_workreportrecord_odoo_api.dart';
import 'package:flutter_admin/api/odoo/stock_quant_odoo_api.dart';
import 'package:flutter_admin/models/data_image_model.dart';
import 'package:flutter_admin/models/odoo/stock_quant.dart';
import 'package:flutter_admin/models/user_info.dart';
import 'package:flutter_admin/pages/common/cry/model/response_body_api.dart';
import 'package:flutter_admin/utils/store_util.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import 'cloumn_chart.dart';
import 'data_grid.dart';

class WorkshopScreen extends StatefulWidget {
  WorkshopScreen({Key? key}) : super(key: key);

  @override
  _WorkshopScreenState createState() => _WorkshopScreenState();
}

class _WorkshopScreenState extends State<WorkshopScreen> {
  List<Map> data1 = [];
  List<Map> data2 = [];
  List<Map> data3 = [];
  List<Map> data4 = [];
  List<Map> data5 = [];
  UserInfo userInfo = StoreUtil.getCurrentUserInfo();
  String workshopCode = '';
  Duration duration = Duration(seconds: 60);
  late Timer _timer;
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance!.addPostFrameCallback((c) {
      _loadData();
    });
    _timer = Timer.periodic(duration, (timer) {
      _loadData();
    });
  }

  @override
  void dispose() {
    _timer.cancel();
    super.dispose();
  }

  _loadData() async {
    ResponseBodyApi res = await ProductWorkreportrecordOdooApi.getScreen(data: [['plant','=',int.parse(userInfo.deptId!)]]);
    ResponseBodyApi res1 = await DynamicDataApi.getModels(model: 'hr.department',key: 'id',val: userInfo.deptId);
    if(res1.success!){
      workshopCode = res1.data[0]['department_code'];
    }
    if(res.success!){
      var data = res.data[0];
      data1 = [
        {'x':'计划工单','y':data['plan_data']['plan']},
        {'x':'已完成工单','y':data['plan_data']['finish']},
        {'x':'完成率','y':data['plan_data']['accomplish']},
      ];
      for(var element in data['work_team']){
        data2.add({'x':element['team_name'],'y':element['qualified_team']});
      }
      data3 = [
        {'x':'设备总数','y':data['work_equip']['facility']},
        {'x':'开工数','y':data['work_equip']['employ']},
        {'x':'运行率','y':data['work_equip']['HUT']},
      ];
      for(var element in data['unqualified_data']){
        data4.add({'x':element['team_name'],'y':element['num']});
      }
      for(var element in data['work_data']){
        data5.add({
          'productionorderno':element['productionorderno']!=false?element['productionorderno']:'',
          'productName':element['product_id']!=false?element['product_id']['name']:'',
          'processName':element['process_id']!=false?element['process_id'][1]:'',
          'teamName':element['team_id']!=false?element['team_id'][1]:'',
          'reportworkerName':element['reportworker_id']!=false?element['reportworker_id'][1]:'',
          'qualifiedquantity':element['qualifiedquantity']!=false?element['qualifiedquantity'].toString():'',
          'reportworkerDate':element['reportdate']!=false?element['reportdate'].toString():'',
        });
      }
    }
    // data2 = [
    //   {'x':'喷涂1','y':1},
    //   {'x':'喷涂2','y':2},
    //   {'x':'喷涂3','y':3},
    //   {'x':'喷涂4','y':4},
    //   {'x':'喷涂5','y':5},
    // ];
    // data5.add({
    //   'teamName':'喷涂',
    //   'reportworkerName':'admin',
    //   'qualifiedquantity':'10',
    // });
    // data5.add({
    //   'teamName':'喷涂',
    //   'reportworkerName':'admin',
    //   'qualifiedquantity':'10',
    // });
    // data5.add({
    //   'teamName':'喷涂',
    //   'reportworkerName':'admin',
    //   'qualifiedquantity':'10',
    // });
    // data5.add({
    //   'teamName':'喷涂',
    //   'reportworkerName':'admin',
    //   'qualifiedquantity':'10',
    // });
    // data5.add({
    //   'teamName':'喷涂',
    //   'reportworkerName':'admin',
    //   'qualifiedquantity':'10',
    // });
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Center(
      child: Container(
        decoration: BoxDecoration(
          image: DecorationImage(
            image: AssetImage("assets/bg/bg1.png"),
            fit: BoxFit.cover,
          ),
        ),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Padding(
              padding: EdgeInsets.all(10.r),
              child: Text(
                "${userInfo.deptName}看板",
                style: TextStyle(
                    fontSize: 36.sp,
                    decoration: TextDecoration.none,
                    color: Color.fromRGBO(255, 255, 255, 1)),
              ),
            ),
            Expanded(
              child: Column(
                children: [
                  Expanded(
                    child: Row(
                      children: [
                        Expanded(child: CloumnChart(dataList: data1,title: '计划工单完成率',),),
                        Expanded(child: CloumnChart(dataList: data2,title: '本车间报工数量（按班组）',),)
                      ],
                    )
                  ),
                  workshopCode=='BJ'?Expanded(
                    child: Row(
                      children: [
                        Expanded(child: CloumnChart(dataList: data3,title: '设备运行率',)),
                        Expanded(child: CloumnChart(dataList: data4,title: '本车间不合理数量',)),
                      ],
                    ),
                  ):Container(),
                  Expanded(
                    child: DataGtid(dataList: data5, title: '${userInfo.deptName}报工统计',),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
